﻿/// <reference name="MicrosoftAjax.js" />
// Register the namespace for the control.
Type.registerNamespace('Medianamik');

Medianamik.LockRequester = function Medianamik$LockRequester(element) {
    Medianamik.LockRequester.initializeBase(this, [element]);

    this._requestLockResult = function(result, info) {
        if (result == 1) {
            if (info.requestSucceededCallback && typeof (info.requestSucceededCallback) == 'function') {
                info.requestSucceededCallback(info.id);
            }
        }
        else {
            if (info.requestFailedCallback && typeof (info.requestFailedCallback) == 'function') {
                info.requestFailedCallback(info.id);
            }
        }
    }

    this._checkLockResult = function(result, info) {
        if (result == 1) {
            if (info.lockedCallback && typeof (info.lockedCallback) == 'function') {
                info.lockedCallback(info.id);
            }
        }
        else {
            if (info.notLockedCallback && typeof (info.notLockedCallback) == 'function') {
                info.notLockedCallback(info.id);
            }
        }
    }
    

    this._requestError = function(error) {
        alert("Service Error:" + error.get_message());
    }
}

function Medianamik$LockRequester$initialize() {
    Medianamik.LockRequester.callBaseMethod(this, 'initialize');
}

function Medianamik$LockRequester$dispose() {
    $clearHandlers(this.get_element());
    Medianamik.LockRequester.callBaseMethod(this, 'dispose');
}

function Medianamik$LockRequester$tryLock(id, ultimatum, requestSucceededCallback, requestFailedCallback) {
    Medianamik.UI.Services.LockWebService.Lock(id, ultimatum, Function.createDelegate(this, this._requestLockResult), Function.createDelegate(this, this._requestError), { id: id, requestSucceededCallback: requestSucceededCallback, requestFailedCallback: requestFailedCallback });
}

function Medianamik$LockRequester$checkLock(id, lockedCallback, notLockedCallback) {
    Medianamik.UI.Services.LockWebService.Check(id, Function.createDelegate(this, this._checkLockResult), Function.createDelegate(this, this._requestError), { id: id, lockedCallback: lockedCallback, notLockedCallback: notLockedCallback });
}

function Medianamik$LockRequester$unlock(id) {
    Medianamik.UI.Services.LockWebService.Unlock(id);
}

Medianamik.LockRequester.prototype = {
    initialize: Medianamik$LockRequester$initialize,
    dispose: Medianamik$LockRequester$dispose,
    tryLock: Medianamik$LockRequester$tryLock,
    checkLock: Medianamik$LockRequester$checkLock,
    unlock: Medianamik$LockRequester$unlock
}

// Register the class as a type that inherits from Sys.UI.Control.
Medianamik.LockRequester.registerClass('Medianamik.LockRequester', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

